/*
 * JFlubberMainFrm.java
 *
 * Created on February 26, 2006, 11:00 AM
 *
 * Released under the GPL by Dick Wall - February 2006
 */

package org.bldc.jflubber;

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Timer;
import java.util.TimerTask;
import javax.imageio.ImageIO;
import javax.swing.JFileChooser;
import javax.swing.JPanel;

/**
 *
 * @author  dick
 */
public class JFlubberMainFrm extends javax.swing.JFrame {
    
    private Timer timer = null;
    private int labelNo = 0;
    private long currTime = 0L;
    private long startTime = 0L;

    class UpdateTimeTask extends TimerTask {
        public void run() {
            long millis = System.currentTimeMillis() - startTime;
            int seconds = (int)(millis/1000);
            int minutes = seconds/60;
            seconds = seconds % 60;
            
            timeLabel.setText(String.format("%d:%02d", minutes, seconds));
        }
    }
    
    private static class WoodenContentPane extends JPanel {
        private static boolean isInitialized = false;
        private static BufferedImage background = null;
        
        private WoodenContentPane() {
            if (!isInitialized) {
                try {
                    background = ImageIO.read(getClass().getResource("images/wood.png"));
                    isInitialized = true;
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        }
        
        @Override
        protected void paintComponent(Graphics g) {
            if (isInitialized) {
                g.drawImage(background, 0, 0, null);
            }
        }
    }
    
    /** Creates new form JFlubberMainFrm */
    public JFlubberMainFrm() {
        setContentPane(new WoodenContentPane());
        initComponents();
        //setSize(300, 460);
        setLocationRelativeTo(null);
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        startButton = new javax.swing.JButton();
        stopButton = new javax.swing.JButton();
        flubButton = new javax.swing.JButton();
        saveButton = new javax.swing.JButton();
        clearButton = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        flubPointsTextArea = new javax.swing.JTextArea();
        timeLabel = new org.bldc.jflubber.TimeLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("jFlubber");
        setResizable(false);
        startButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/bldc/jflubber/images/start-button.png")));
        startButton.setBorderPainted(false);
        startButton.setContentAreaFilled(false);
        startButton.setFocusPainted(false);
        startButton.setPressedIcon(new javax.swing.ImageIcon(getClass().getResource("/org/bldc/jflubber/images/start-button-pressed.png")));
        startButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                startButtonMouseReleased(evt);
            }
        });

        stopButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/bldc/jflubber/images/stop-button.png")));
        stopButton.setBorderPainted(false);
        stopButton.setContentAreaFilled(false);
        stopButton.setFocusPainted(false);
        stopButton.setPressedIcon(new javax.swing.ImageIcon(getClass().getResource("/org/bldc/jflubber/images/stop-button-pressed.png")));
        stopButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                stopButtonMouseReleased(evt);
            }
        });

        flubButton.setFont(new java.awt.Font("Dialog", 1, 18));
        flubButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/bldc/jflubber/images/flub-button.png")));
        flubButton.setBorderPainted(false);
        flubButton.setContentAreaFilled(false);
        flubButton.setFocusPainted(false);
        flubButton.setPreferredSize(new java.awt.Dimension(90, 91));
        flubButton.setPressedIcon(new javax.swing.ImageIcon(getClass().getResource("/org/bldc/jflubber/images/flub-button-pressed.png")));
        flubButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                flubButtonMouseReleased(evt);
            }
        });

        saveButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/bldc/jflubber/images/save-button.png")));
        saveButton.setBorderPainted(false);
        saveButton.setContentAreaFilled(false);
        saveButton.setFocusPainted(false);
        saveButton.setPressedIcon(new javax.swing.ImageIcon(getClass().getResource("/org/bldc/jflubber/images/save-button-pressed.png")));
        saveButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                saveButtonMouseReleased(evt);
            }
        });

        clearButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/bldc/jflubber/images/clear-button.png")));
        clearButton.setBorderPainted(false);
        clearButton.setContentAreaFilled(false);
        clearButton.setFocusPainted(false);
        clearButton.setPressedIcon(new javax.swing.ImageIcon(getClass().getResource("/org/bldc/jflubber/images/clear-button-pressed.png")));
        clearButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                clearButtonMouseReleased(evt);
            }
        });

        flubPointsTextArea.setColumns(20);
        flubPointsTextArea.setRows(5);
        jScrollPane1.setViewportView(flubPointsTextArea);

        timeLabel.setText("0:00");

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                    .add(jScrollPane1)
                    .add(timeLabel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 255, Short.MAX_VALUE)
                    .add(layout.createSequentialGroup()
                        .add(startButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 78, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .add(flubButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(stopButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 77, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED))
                    .add(layout.createSequentialGroup()
                        .add(saveButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 79, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .add(clearButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 78, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(timeLabel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 79, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(30, 30, 30)
                        .add(flubButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 99, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(layout.createSequentialGroup()
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(stopButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 37, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(startButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 35, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 146, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(saveButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 33, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(clearButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 34, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(15, Short.MAX_VALUE))
        );
        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void clearButtonMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_clearButtonMouseReleased
        this.flubPointsTextArea.setText("");
        this.labelNo = 0;
        this.timeLabel.setText("0:00");
    }//GEN-LAST:event_clearButtonMouseReleased

    private void saveButtonMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_saveButtonMouseReleased
        JFileChooser chooser = new JFileChooser();
        int rVal = chooser.showSaveDialog(this);
        if(rVal == JFileChooser.APPROVE_OPTION) {
            File outputFile = chooser.getSelectedFile();
            PrintWriter outputStream;
            
            try {
                outputStream = new PrintWriter(new BufferedWriter(new FileWriter(outputFile)));
                outputStream.print(this.flubPointsTextArea.getText());
                outputStream.close();
            }
            catch(IOException ex) {
                ex.printStackTrace();
            }
        }
    }//GEN-LAST:event_saveButtonMouseReleased

    private void flubButtonMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_flubButtonMouseReleased
        labelNo++;
        this.addLabel(Integer.toString(labelNo));
    }//GEN-LAST:event_flubButtonMouseReleased

    private void stopButtonMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_stopButtonMouseReleased
        if(timer != null) {
            timer.cancel();
            startTime = 0L;
        }
    }//GEN-LAST:event_stopButtonMouseReleased

    private void startButtonMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_startButtonMouseReleased
        if(startTime == 0L) {
            startTime = evt.getWhen();
            timer = new Timer();
            timer.schedule(new UpdateTimeTask(), 200, 200);
        }
    }//GEN-LAST:event_startButtonMouseReleased
    
    /**
     * add a label with the current string using the current relative time
     * @param label String containing label to add
     */
    public void addLabel(String label) {
        double seconds = 0.0;
        if(startTime > 0L) {
            long millis = System.currentTimeMillis() - startTime;
            seconds = (double)millis / 1000;            
        }
        String labelList = this.flubPointsTextArea.getText();
        labelList = labelList + Double.toString(seconds) + "\t" + 
				Double.toString(seconds) + "\t" + label + "\n";
        this.flubPointsTextArea.setText(labelList);
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new JFlubberMainFrm().setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton clearButton;
    private javax.swing.JButton flubButton;
    private javax.swing.JTextArea flubPointsTextArea;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton saveButton;
    private javax.swing.JButton startButton;
    private javax.swing.JButton stopButton;
    private org.bldc.jflubber.TimeLabel timeLabel;
    // End of variables declaration//GEN-END:variables
    
}
